//
//  AddUrgeViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/14.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class AddUrgeViewController: BaseViewController {

    var id = ""
    var delegate: ShouldReceiveDetailViewController?
    
    @IBOutlet weak var optionsField: XXTextView!
    @IBAction func ensureAction(_ sender: Any) {
        self.load(with: names[0])
    }
    @IBAction func cancelAction(_ sender: Any) {
        self.dismiss()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBOutlet weak var trayView: UIView!
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let height = self.view.width * 329 / 375
        let animation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: self.view.height - height), size: CGSize(width: self.view.width, height: height)))
        animation?.springSpeed = 50
        animation?.springBounciness = 5
        self.trayView.pop_add(animation, forKey: "t")
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss()
    }
    
    override var names: Array<String>{
        get{
            return ["Submit"]
        }
    }
    
    override func path(for name: String) -> String {
        return UpdateIncomeDynammic
    }
    
    override func strategy(for name: String) -> (Bool, String?, (() -> Void)?) {
        if optionsField.text.count == 0 {
            return (false,"催款反馈不能为空",nil)
        }
        return (true,nil,nil)
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["pid": self.id,"feedback": optionsField.text]
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if json.contents?["success"]?.bool == true {
                //
                self.delegate?.load(with: self.delegate?.names[2] ?? "")
                self.dismiss()
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        return suc
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
